package com.gable.report.controller;

import com.gable.report.dao.IReportAuthDao;
import com.gable.report.dao.IUserRegionDao;
import com.gable.report.model.ReportAuth;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalOverviewService;
import com.gable.report.service.SetRepeatRepairService;
import com.gable.report.thread.SetRepairRepeatThread;
import com.gable.report.util.ChartsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("/index") /* 建议每个Controller都加上@RequestMapping采用Restful风格URL */
public class IndexController {
	
	private final Log logger = LogFactory.getLog(this.getClass());

	@Autowired
	ChartsForHospitalOverviewService chartsForHospitalOverviewService;

	@Autowired
	SetRepeatRepairService setRepeatRepairService;

	@Autowired
	IUserRegionDao userRegionDao;

	@Resource
	HttpServletRequest request;
	
	@Resource
	HttpServletResponse response;

	@Autowired
	IReportAuthDao reportAuthDao;

	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/page",method=RequestMethod.GET)
	public String page(Model model){
		User user = (User)request.getSession().getAttribute("loginUser");
		//区报表
		if(user.getGovernmentType() != null && user.getGovernmentType() > 3){
			Long regionCode= userRegionDao.queryRegionCodeByUserId(user.getId());
			if(regionCode == null){
				return "charts-authority";
			}
			ChartsUtil.userRegionMap.put(user.getId(),regionCode);
			//设置区域重复维修
			new Thread(new SetRepairRepeatThread(setRepeatRepairService,regionCode,2)).start();
			return "charts/area/allocation-hospital";
		}
		if(user.getHospitalId() != null){
			//chartsForHospitalOverviewService.addMemberForGroup(user.getHospitalId(),user.getId());
			ReportAuth reportAuth = reportAuthDao.queryByHospitalId(user.getHospitalId());
			model.addAttribute("userId",user.getId());
			model.addAttribute("hospitalId",user.getHospitalId());
			//设置医院重复维修
			new Thread(new SetRepairRepeatThread(setRepeatRepairService,user.getHospitalId(),1)).start();
			//无权限查看运营管理
			if(reportAuth.getOperationTag() == 0){
				model.addAttribute("auth",0);
				return "/charts/hospital/department-index";
			}else{
				model.addAttribute("auth",1);
				return "/charts/hospital/overview";
			}

		}
        return "charts-authority";
	}
	
}
